import 'package:flutter_demo/core/common/model/creative_model.dart';
import 'package:flutter_demo/core/common/model/ui_element_model.dart';
import 'package:json_annotation/json_annotation.dart';

@JsonSerializable()
class PageState {
  static final empty = PageState(<Component>[]);
  static final error = PageState(<Component>[]);
  static final loding = PageState(<Component>[]);

  @JsonKey(name: 'components')
  List<Component> components;

  PageState(
    this.components,
  );
  //根据 json 创建对象
  factory PageState.fromJson(Map<String, dynamic> srcJson) =>
      _$ListPageStateFromJson(srcJson);
}

PageState _$ListPageStateFromJson(Map<String, dynamic> json) =>
    PageState((json['components'] as List<dynamic>)
        .map((e) => Component.fromJson(e as Map<String, dynamic>))
        .toList());

class Component extends Object {
  late String blockCode;
  late String showType;
  late dynamic extInfo;
  //ui
  @JsonKey(name: 'uiElement')
  late UiElementModel? uiElement;

  //作品
  @JsonKey(name: 'creatives')
  late List<CreativeModel>? creatives;

  Component(this.blockCode, this.showType, this.extInfo, this.uiElement,
      this.creatives);

  factory Component.fromJson(Map<String, dynamic> srcJson) =>
      _$BlocksFromJson(srcJson);

  Component? elementAt(int index) {}
}

Component _$BlocksFromJson(Map<String, dynamic> json) {
  late List<CreativeModel> temps = <CreativeModel>[];
  if (json['creatives'] != null) {
    json['creatives'].forEach((v) {
      temps.add(new CreativeModel.fromJson(v));
    });
  }

  return Component(
    json['blockCode'] as String,
    json['showType'] as String,
    json['extInfo'] as dynamic,
    UiElementModel.fromJson(json['uiElement']),
    temps,
  );
}
